<template>
  <MesBox :title="title" v-model="visible" :width="1200" :height="600">
    <MesForm
      ref="form"
      :formFields="formfields"
      :formRules="formRules"
    ></MesForm>
    <MesTable :tableData="tableData" :columns="columns" :ck="false"></MesTable>
    <template #footer>
      <el-button type="primary" @click="printData">
        <i class="el-icon-printer"></i>
        打印
      </el-button>
      <el-button type="danger" @click="saveData">
        <i class="el-icon-success"></i>
        保存
      </el-button>
    </template>
  </MesBox>
</template>

<script setup>
import MesBox from '@/components/basic/MesBox.vue'
import MesForm from '@/components/basic/MesForm.vue'
import MesTable from '@/components/basic/MesTable.vue'
import { ref, getCurrentInstance } from 'vue'
import { getNowDate } from '@/uitils/dateFormatUtil'

const emits = defineEmits(['parentCall'])
const that = getCurrentInstance().appContext.config.globalProperties
const form = ref(null)
let visible = ref(false)
let dialogVisible = ref(false)
let print = null
let tableData = ref([])
let title = ref('')
let columns = [
  {
    title: '产品名称',
    field: 'productName',
  },
  {
    title: '产品编码',
    field: 'productCode',
  },
  {
    title: '规格',
    field: 'specification',
  },
  {
    title: '单位',
    field: 'Unit',
  },
  {
    title: '数量',
    field: 'qnty',
  },
]
let formfields = ref({
  productName: '',
  productCode: '',
  createDate: getNowDate(),
  qty: '',
  loss: '',
  grossWeight: '',
  singleWeight: '',
  userName: '',
  senderName: '',
  workOrderList_Id: '',
})
let formRules = [
  [
    {
      title: '产品名称',
      field: 'productName',
      disabled: true,
    },
    {
      title: '产品编码',
      field: 'productCode',
      disabled: true,
    },
    {
      title: '日期',
      field: 'createDate',
      disabled: true,
    },
  ],
  [
    {
      title: '工单数量',
      field: 'qty',
      disabled: true,
    },
    {
      title: '损耗',
      field: 'loss',
      disabled: true,
    },
    {
      title: '毛重',
      field: 'grossWeight',
      disabled: true,
    },
  ],
  [
    {
      title: '单重',
      field: 'singleWeight',
      disabled: true,
    },
    {
      title: '领料人',
      field: 'userName',
      required: true,
    },
    {
      title: '发料人',
      field: 'senderName',
      required: true,
    },
  ],
  [
    {
      title: '任务单ID',
      field: 'workOrderList_Id',
      required: true,
      type: 'select',
      dataKey: 'workOrderListId',
    },
  ],
]

const open = (tabledata, printFun, SuperiorInfo, sendTitle) => {
  print = printFun
  tableData.value = tabledata
  title.value = sendTitle
  for (let key in SuperiorInfo) {
    formfields.value[key] = SuperiorInfo[key]
  }

  visible.value = true
}

const saveData = () => {
  form.value.validate(async (valid) => {
    if (valid) {
      console.log(tableData.value)
      tableData.value = tableData.value.map((item) => {
        return {
          ...item,
          workOrderList_Id: formfields.value.workOrderList_Id,
        }
      })
      let res = await that.http.post(
        'api/Production_WorkOrder/InsertOutRawMaterial1',
        { addOutRawMaterial: tableData.value },
        true
      )

      if (res.status) {
        let msg = title.value.substring(2)
        that.$message.success(`${msg}开具成功`)
        visible.value = false
        dialogVisible.value = true
        emits('parentCall', (vue) => {
          vue.search()
        })
      } else {
        this.$message.error(res.message)
      }
    }
  })
}

const printData = () => {
  form.value.validate((valid) => {
    if (valid) {
      for (let key in formfields.value) {
        if (formfields.value[key] == null) {
          formfields.value[key] = ''
        }
      }
      let data = {
        mainData: formfields.value,
        detailData: tableData.value,
      }
      let printTitle = title.value.substring(2)
      print(data, printTitle)
    }
  })
}

defineExpose({ open })
</script>

<style lang="less" scoped></style>
